package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

/**
 *  IqiluNewsAnalyse  齐鲁网新闻解析类
 *  xiaonie 2012-11-22
 */
public class IqiluNewsAnalyse implements AnalyseNews{

	private final String[] regexs={ "http://[a-z]*[.]iqilu.com/[a-z]*/[a-z]*/[0-9]*/[0-9]*[.]shtml",
			"http://[a-z]*[.]iqilu.com/[a-z]*/[0-9]*/[0-9]*/[0-9]*[.]shtml",
			"http://[a-z]*[.]iqilu.com/[a-z]*/[0-9]{8}/[0-9]+.shtml",
			"http://[a-z]*[.]iqilu.com/[a-z]*/[a-z]*/[0-9]*/[0-9]*/[0-9]*[.]shtml",
			"http://[a-z]*[.]iqilu.com/[a-z]*/[a-z]*/[a-z]*/[0-9]{4}/[0-9]{4}/[0-9]+.shtml",
			};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			title=doc.select("div#city_nr_left_cnt h1").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("h1").text();
			}

			doc.select(".moreread,div#right").remove();

			content=HtmlCleaner.getContentHtml(url,doc.select("div.article-main"));

			date=DateUtils.matchDate(doc.select("#pubtime_baidu").text());
			if(date==null){
				date=DateUtils.matchDate(url);
			}
			news.setContent(content);
			news.setTitle(title);
			news.setClickNum(null);
			news.setCommentNum(null);
			news.setDate(date);
			news.setType(1);

			String author=JSoupUtils.matchAuthor(doc, "来源:");
			news.setAuthor(author);
		}
		return news;
	}

	public static void main(String[] args) {
		IqiluNewsAnalyse a=new IqiluNewsAnalyse();
		String url="http://qingdao.iqilu.com/qdyaowen/2016/1108/3158911.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
